rm(list = ls())
require(stargazer)


data2old=read.csv("D:/Documents and Work/Dean Stuff/data/infoexp2_data/responses.csv", header=TRUE)
data13=read.csv("D:/Documents and Work/Dean Stuff/data/columbia_experiment_july25_type13clean.csv", header=TRUE)
data15=read.csv("D:/Documents and Work/Dean Stuff/data/columbia_experiment_August12_type15(clean).csv", header=TRUE)


dataframe2old=data.frame(data2old)
dataframe13=data.frame(data13)
dataframe15=data.frame(data15)


priors=c(0.5,0.6,0.75,0.85)

expframe=rbind(dataframe13,dataframe15,dataframe2old)
expframe=subset(expframe,uid>200)

#note that player 234 did not finish and so has no real data
expframe=subset(expframe,uid!=234)


#definitions
expframe$priorA=(expframe$qid==1)*0.5+(expframe$qid==5)*0.6+(expframe$qid==6)*0.75+(expframe$qid==7)*0.85
expframe$priorB=1-expframe$priorA
expframe$Aresponse=(expframe$response==6)
expframe$Bresponse=(expframe$response==7)
expframe$Astate=(expframe$state==4)
expframe$Bstate=(expframe$state==10)
expframe$correct=expframe$Astate*expframe$Aresponse+expframe$Bstate*expframe$Bresponse

#adjust values based on priors
#correct
avgcorrect=sum(expframe$correct)/length(expframe$correct)
avgcorrect0.5=(expframe$priorA==0.5)*(sum(expframe$correct*(expframe$priorA==0.5))/sum(expframe$priorA==0.5))
avgcorrect0.6=(expframe$priorA==0.6)*sum(expframe$correct*(expframe$priorA==0.6))/sum(expframe$priorA==0.6)
avgcorrect0.75=(expframe$priorA==0.75)*sum(expframe$correct*(expframe$priorA==0.75))/sum(expframe$priorA==0.75)
avgcorrect0.85=(expframe$priorA==0.85)*sum(expframe$correct*(expframe$priorA==0.85))/sum(expframe$priorA==0.85)
expframe$correctmod=expframe$correct+avgcorrect-avgcorrect0.5-avgcorrect0.6-avgcorrect0.75-avgcorrect0.85
uidlist=unique(expframe$uid)


N=1000
n=length(uidlist)
randomdraw=sample(1:n,n*N,replace=TRUE)

probcorrectblock2ef=vector('numeric')
probcorrectblock3ef=vector('numeric')
probcorrectblock4ef=vector('numeric')

for(i in 1:N){
bootframe=data.frame()
for(j in 1:n){
addframe=subset(expframe,uid==uidlist[randomdraw[(i-1)*n+j]])
bootframe=rbind(bootframe,addframe)
}
bootframe1=subset(bootframe,blocknum==1)
bootframe2=subset(bootframe,blocknum==2)
bootframe3=subset(bootframe,blocknum==3)
bootframe4=subset(bootframe,blocknum==4)
probcorrectblock2ef[i]=sum(bootframe2$correct)/length(bootframe2$correct)-sum(bootframe1$correct)/length(bootframe1$correct)
probcorrectblock3ef[i]=sum(bootframe3$correct)/length(bootframe3$correct)-sum(bootframe1$correct)/length(bootframe1$correct)
probcorrectblock4ef[i]=sum(bootframe4$correct)/length(bootframe4$correct)-sum(bootframe1$correct)/length(bootframe1$correct)

}
#clean
probcorrectblock2ef=na.omit(probcorrectblock2ef)
probcorrectblock3ef=na.omit(probcorrectblock3ef)
probcorrectblock4ef=na.omit(probcorrectblock4ef)

lower2=round(length(probcorrectblock2ef)*0.025,1)
lower3=round(length(probcorrectblock3ef)*0.025,1)
lower4=round(length(probcorrectblock4ef)*0.025,1)

upper2=length(probcorrectblock2ef)-lower2
upper3=length(probcorrectblock3ef)-lower3
upper4=length(probcorrectblock4ef)-lower4


sortprobcorrectblock2ef=sort(probcorrectblock2ef)
sortprobcorrectblock3ef=sort(probcorrectblock3ef)
sortprobcorrectblock4ef=sort(probcorrectblock4ef)


avgprobcorrectblock2ef=mean(probcorrectblock2ef)
avgprobcorrectblock3ef=mean(probcorrectblock3ef)
avgprobcorrectblock4ef=mean(probcorrectblock4ef)

upperprobcorrectblock2ef=sortprobcorrectblock2ef[upper2]
upperprobcorrectblock3ef=sortprobcorrectblock3ef[upper3]
upperprobcorrectblock4ef=sortprobcorrectblock4ef[upper4]


lowerprobcorrectblock2ef=sortprobcorrectblock2ef[lower2]
lowerprobcorrectblock3ef=sortprobcorrectblock3ef[lower3]
lowerprobcorrectblock4ef=sortprobcorrectblock4ef[lower4]

i=1
while((sortprobcorrectblock2ef[i]<0)&(sortprobcorrectblock2ef[length(sortprobcorrectblock2ef)+1-i]>0)){
i=i+1
}
pvalblock2=2*i/N
i=1
while((sortprobcorrectblock3ef[i]<0)&(sortprobcorrectblock3ef[length(sortprobcorrectblock3ef)+1-i]>0)){
i=i+1
}
pvalblock3=2*i/N
i=1
while((sortprobcorrectblock4ef[i]<0)&(sortprobcorrectblock4ef[length(sortprobcorrectblock4ef)+1-i]>0)){
i=i+1
}
pvalblock4=2*i/N


orderreport=data.frame(average=c(avgprobcorrectblock2ef,avgprobcorrectblock3ef,avgprobcorrectblock4ef),lowerCI=c(lowerprobcorrectblock2ef,lowerprobcorrectblock3ef,lowerprobcorrectblock4ef),upperCI=c(upperprobcorrectblock2ef,upperprobcorrectblock3ef,upperprobcorrectblock4ef),Pval=c(pvalblock2,pvalblock3,pvalblock4))
rownames(orderreport)=c("Block 2","Block 3","Block 4")

#TableA 3.2*********************************
stargazer(orderreport,summary=FALSE)
#*******************************************
